package com.sskj.mine.presenter;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.ui.activity.LoginActivity;


public class LoginActivityPresenter extends BasePresenter<LoginActivity> {


    /**
     * 登录
     *
     * @param mobile 手机号
     * @param opwd   密码
     * @return
     */
    public void login(String mobile, String opwd) {
        httpService.login(mobile, opwd)
                .execute(new JsonCallBack<HttpData<UserData>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<UserData>> response) {
                        HttpData<UserData> httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            SPUtil.put(SPConfig.ACCOUNT, httpData.getData().getAccount());

                            SPUtil.put(SPConfig.SESSIONID, httpData.getData().getSessionId());
                            SPUtil.put(SPConfig.ID, httpData.getData().getId() + "");
                            HttpParams httpParams = new HttpParams();
                            httpParams.put("systemType", "android");
                            httpParams.put("account", httpData.getData().getAccount());    //所有的 header 都 不支持 中文
                            httpParams.put("sessionId", httpData.getData().getSessionId());
                            httpParams.put("id", httpData.getData().getId());
                            OkGo.getInstance().addCommonParams(httpParams);
                            mView.loginSuccess();

//                            if (httpData.getData().isGoogleAuthentication()) {
//                                mView.googleCheck(httpData.getData().getId() + "");
//                            } else {
//                                mView.loginSuccess();
//                            }
                        }
                    }
                });
    }


    /**
     * Google验证登录
     *
     * @param code        随机验证码
     * @param stockUserId 当前登录用户ID
     * @return
     */
    public void googleAuthVerify(String code, String stockUserId) {
        httpService.googleAuthVerify(code, stockUserId)
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.loginSuccess();
                        }
                    }
                });
    }

}
